package com.hanxiaozhang.base.controller;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.hanxiaozhang.result.Result;
import com.hanxiaozhang.util.PageUtil;
import com.hanxiaozhang.validation.util.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanxiaozhang.base.entity.DictEntity;
import com.hanxiaozhang.base.service.DictService;


/**
 * 数据字典
 *
 * @author hanxiaozhang
 * @email hanxiaozhang2018@sina.com
 * @date 2021-04-07
 */
@Slf4j
@Controller
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     * 查询列表
     *
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('base:dict:list')")
    public Result<PageUtil> list(@RequestParam Map<String, Object> params) {

        Page<DictEntity> page = PageHelper
                .startPage(Integer.parseInt(params.get("current").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<DictEntity> dictList = dictService.list(params);
        return Result.success(new PageUtil(dictList, (int) page.getTotal()));
    }


    /**
     * 保存
     *
     * @param dict
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('base:dict:save')")
    public Result<Void> save(DictEntity dict) {
        valid(dict, true);
        dictService.save(dict);
        return Result.success();
    }


    /**
     * 修改
     *
     * @param dict
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('base:dict:update')")
    public Result<Void> update(DictEntity dict) {
        valid(dict, false);
        dictService.update(dict);
        return Result.success();
    }

    /**
     * 参数校验
     *
     * @param dict
     * @param saveFlag
     */
    private void valid(DictEntity dict, boolean saveFlag) {

        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(dict.getName()), "标签名不能为空");
        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(dict.getValue()), "标签值不能为空");
        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(dict.getType()), "类型不能为空");
        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(dict.getTypeName()), "类型名称不能为空");
    }

    /**
     * 删除
     *
     * @param dictId
     * @param type
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    @PreAuthorize("hasAuthority('base:dict:remove')")
    public Result<Void> remove(Long dictId, String type) {

        dictService.remove(dictId, type);
        return Result.success();
    }


    /**
     * 批量删除
     *
     * @param dictIds
     * @param types
     * @return
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @PreAuthorize("hasAuthority('base:dict:batchRemove')")
    public Result<Void> remove(Long[] dictIds, String[] types) {

        dictService.batchRemove(dictIds,types);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param type
     * @return
     */
    @GetMapping("/listByType")
    @ResponseBody
    public Result listByType(String type) {

        return Result.success(dictService.listByType(type));
    }




}
